package leetcode;

import javax.swing.tree.TreeNode;
import java.util.ArrayList;
import java.util.List;

public class Solution5 {
    public List<List<Integer>> resList = new ArrayList<List<Integer>>();

    public List<List<Integer>> levelOrder(TreeNode root) {
        checkFind(root);

        return resList;
    }


    public void checkFind(TreeNode node){
        if(node == null){
            return ;
        }

        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(node);

        while(!queue.isEmpty()){
            //用来存储遍历之后的数据
            List<Integer> itemList = new ArrayList<Integer>();
            int len = queue.size();

            while(len > 0){
                TreeNode tempNode = queue.poll();
                itemList.add(tempNode.val);

                if(tempNode.left != null){
                    queue.offer(tempNode.left);
                }
                if(tempNode.right != null){
                    queue.offer(tempNode.right);
                }
                len--;
            }

            resList.add(itemList);
        }
    }

}
